/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import com.lineage.server.model.L1Character;
import com.lineage.server.model.Instance.L1MonsterInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.packetserver.S_ServerMessage;

/**
 * Class <code>TameMonster</code> 法师魔法:迷魅术 (30/20/1).
 * 有一定机率使怪物跟随施法者，并接受其命令，怪物体力越低越易成功，但某些怪物是免疫的
 * 
 * @author jrwz
 * @version 2012-7-24下午08:56:12
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class TameMonster extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new TameMonster();
    }

    /** 法师魔法:迷魅术 (30/20/1). */
    private TameMonster() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        L1PcInstance player = null;
        if (user instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) user;
            player = pc;
        }
        if (cha instanceof L1MonsterInstance) {
            final L1MonsterInstance npc = (L1MonsterInstance) cha;
            // 可迷魅的怪物
            if (npc.getNpcTemplate().isTamable()) {
                int petcost = 0;
                final Object[] petlist = user.getPetList().values().toArray();
                for (final Object pet : petlist) {
                    petcost += ((L1NpcInstance) pet).getPetcost();
                }
                int charisma = user.getCha();
                if (player.isElf()) { // 精灵
                    if (charisma > 30) { // max count = 7
                        charisma = 30;
                    }
                    charisma += 12;
                } else if (player.isWizard()) { // 法师
                    if (charisma > 36) { // max count = 7
                        charisma = 36;
                    }
                    charisma += 6;
                }
                charisma -= petcost;
                if (charisma >= 6) { // 确保宠物的成本
                    final L1SummonInstance summon = new L1SummonInstance(npc,
                            user, false);
                    target = summon; // 更换目标
                } else {
                    player.sendPackets(new S_ServerMessage(319)); // \f1你不能拥有太多的怪物。
                }
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        // TODO Auto-generated method stub
    }
}
